#! /usr/bin/perl

our @Data;

sub reading {
    while (<>) {
	chomp;
	next unless m/\A-/;
	my ($perm, $size, $date, $time, $name) = split(" ");
	my $body = $name;

	# Remove file name extensions
	$body =~ s/\.(tar|gz|dmg|exe|sh|dmg|bz2)\Z//;
	$body =~ s/\.(tar|gz|dmg|exe|sh|dmg|bz2)\Z//;
	$body =~ s/_(deb|rpm)\Z//;
	$body =~ s/_[a-zA-Z-]+\Z//;
	$body =~ s/_wJRE\Z//;
	$body =~ s/_l10n\Z//;

	# Put minor files into one category
	if ($body =~ m/\ABrOOo_/) {
	    $body =~ s/_[a-zA-Z0-9-]+_[a-zA-Z0-9-]+\Z//;
	}
	if ($body =~ m/\AOOo-SDK_/) {
	    $body =~ s/_[a-zA-Z0-9-]+_[a-zA-Z0-9-]+\Z//;
	}
	if ($body =~ m/\AOOo-Dev-SDK_/) {
	    $body =~ s/_[a-zA-Z0-9-]+_[a-zA-Z0-9-]+\Z//;
	}

	my %entry;
	$entry{name} = $name;
	$entry{size} = $size;
	$entry{body} = $body;
	push @Data, \%entry;
    }
}

sub print_ruler {
    print "=" x (50 + 8 + 8 + 8) . "\n";
}

sub print_header {
    printf( "%-50s %7s %7s %7s\n", "Catetory", "AvgSize", "Count", "TtlSize" );
}

sub print_entry {
    my ($accum, $count, $body) = @_;
    my $unit = 1024 * 1024;
    printf( "%-50s %7d %7d %7d\n", $body, ($accum / $count) / $unit, $count, $accum / $unit );
}

sub printing {
    my $total_size = 0;
    my $total_count = 0;
    my $accum = 0;
    my $last = undef;
    my $count = 0;

    print_header();
    print_ruler();

    foreach $entryref (@Data) {
	my $body = ${$entryref}{body};
	my $size = ${$entryref}{size};
	if (defined($last) and $last ne $body) {
	    print_entry($accum, $count, $last);
	    $accum = 0;
	    $count = 0;
	}
	$total_size += $size;
	$total_count ++;
	$accum += $size;
	$count ++;
	$last = $body;
    }	

    if (0 < $count) {
	print_entry($accum, $count, $last);
    }

    print_ruler();
    print_entry($total_size, $total_count, "Total");
}

sub main {
    reading();
    printing();
}

main();

__END__
